//
//  MainTabBarController.swift
//  LDMicroblog07
//
//  Created by pro on 15/9/15.
//  Copyright (c) 2015年 pro. All rights reserved.
//

import UIKit

class MainTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
       
        //使用KVC 给私有属性赋值
        let tb = MainTabBar()
        setValue(tb, forKey: "tabBar")
        //MARK: 监听 加号  按钮点击
        tb.plusBtn.addTarget(self, action: "plusBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        //Xcode 6使用此方法只能改变文字颜色，图标依旧是蓝色
        /*:Xcode 6要想改变图标颜色，则需要设置图片的模式
            var img = UIImage(named: "tabbar_home_highlighted")
            img = img?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        */
         //Xcode 7 特有的方法 可改变图标和文字颜色
        tabBar.tintColor = UIColor.orangeColor()
        addChildViewControllers()
       
//        print(tabBar)
        //viewdidload方法中items没有值
//        print(tabBar.items)
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
//        print("---------")
        //viewWillAppear方法中items才有值
//        print(tabBar.subviews as? NSArray)
    }
    
    //MARK: 所有子控制器
    private func addChildViewControllers() {
    
        addChildViewController(HomeTableViewController(), title: "首页", imgName: "tabbar_home")
        addChildViewController(DiscoverTableViewController(), title: "发现", imgName: "tabbar_discover")
        addChildViewController(MessageTableViewController(), title: "消息", imgName: "tabbar_message_center")
        addChildViewController(ProfileTableViewController()
            , title: "我", imgName: "tabbar_profile")
    }

    ///  单个控制器
    ///
    ///  :param: vc      控制器
    ///  :param: title   标题
    ///  :param: imgName 图标名

    //MARK: 单个控制器
    private func addChildViewController(vc: UIViewController, title: String, imgName:String) {
    
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imgName)
        vc.tabBarItem.selectedImage = UIImage(named: imgName + "_highlighted")
        let nav = BaseNavigationController(rootViewController: vc)
        addChildViewController(nav)
    }
    
    //加号按钮 点击事件
    func plusBtnClick() {
    
        print(__FUNCTION__)
    }
    //-------------------------------------------------------------------------------
    //命名空间 nameSpace
    func mingMingSpace() {
    
        
        //类名包含在命名空间中，所以需要找出nameSpace
//        print(NSBundle.mainBundle().infoDictionary)
        let namespace = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String
        //获得类名
        let className: AnyClass = NSClassFromString(namespace + "." + "HomeTableViewController")!
        print(className)
        //通过‘类名’创建‘对象’
        let vc = className.alloc() as! UIViewController
        vc.title = "首页"
    }
    //加载json
    func getJsonData() {
    
        //获取路径
        let path = NSBundle.mainBundle().pathForResource("vcData.json", ofType: nil)
        let data = NSData(contentsOfFile: path!)
        //反序列化 throw 是 Xcode 7.0 最明显的一个变化
        do {
        
            //尝试执行代码 获取数据
            let array = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments)
            for dict in array as! [[String: String]] {
                
                print(dict)
                
                let ns = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String
                let vcClass: AnyClass = NSClassFromString(ns + "." + dict["vcName"]!)!
                let vc = vcClass.new() as! UIViewController
                addChildViewController( vc, title: dict["title"]!, imgName: dict["imgName"]!)
            }

        } catch {
        
            //捕获异常
            print(error)
            //获取数据失败 使用本地数据
            addChildViewController(HomeTableViewController(), title: "首页", imgName: "tabbar_home")
            addChildViewController(DiscoverTableViewController(), title: "发现", imgName: "tabbar_discover")
            addChildViewController(MessageTableViewController(), title: "消息", imgName: "tabbar_message_center")
            addChildViewController(ProfileTableViewController()
                , title: "我", imgName: "tabbar_profile")

        }
        
    }
    
    func getJsonData01() {
    
        //获取路径
        let path = NSBundle.mainBundle().pathForResource("vcData.json", ofType: nil)
        let data = NSData(contentsOfFile: path!)
        //反序列化 throw 是xcode 7特有
        do {
        
            let array = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments)
            for dict in array as! [[String: String]] {
                
                let ns = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String
                let vcClassName: AnyClass = NSClassFromString(ns + "." + dict["vcName"]!)!
                let vc = vcClassName.alloc() as! UIViewController
                addChildViewController(vc, title: dict["title"]!, imgName: dict["imgName"]!)
            }
            
        }catch {
        
            print(error)
            //获取数据失败 使用本地数据
            addChildViewController(HomeTableViewController(), title: "首页", imgName: "tabbar_home")
            addChildViewController(DiscoverTableViewController(), title: "发现", imgName: "tabbar_discover")
            addChildViewController(MessageTableViewController(), title: "消息", imgName: "tabbar_message_center")
            addChildViewController(ProfileTableViewController()
                , title: "我", imgName: "tabbar_profile")
            
        }
        
    }
}
